package client

import (
	"crypto/md5"
	"fmt"
	"github.com/gomodule/redigo/redis"
	"time"
)

// 例子
func Example() {
	// 通过连接信息创建客户端
	redis_host := "127.0.0.1"
	redis_port := 6379
	pool_size := 20
	pool := &redis.Pool{
		// Other pool configuration not shown in this example.
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", redis_host, redis_port))
			if err != nil {
				return nil, err
			}
			return c, nil
		},
	   MaxIdle:pool_size,
	}
	// 通过已有连接创建客户端
	cli1 := Client{
		Conn:pool.Get(),
	}
	cli1.Init()
	s, _ := redis.String(cli1.Conn.Do("PING"))
	fmt.Printf("回显:%+v\n",s )  //回显:PONG

}

// Push 例子
func ExampleClient_Push() {
	cli := Client{
		Host:     "127.0.0.1",
		Port:     "6379",
		Database: 0,
		Password: "",
	}
	cli.Init()
	msg := Message{
		ID:    fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()))),
		Topic: "test",
		Body:  "test body",
	}
	reply, err := cli.Push(msg, 10, 600)
	fmt.Println(msg)
	fmt.Println(reply)
	fmt.Println(err)
}

// Pop 例子
func ExampleClient_Pop() {
	cli := Client{
		Host:     "127.0.0.1",
		Port:     "6379",
		Database: 0,
		Password: "",
	}
	cli.Init()
	msg, err := cli.Pop("test");
	fmt.Println(msg)
	fmt.Println(err)
}

// BPop 例子
func ExampleClient_BPop() {
	cli := Client{
		Host:     "127.0.0.1",
		Port:     "6379",
		Database: 0,
		Password: "",
	}
	cli.Init()
	msg, err := cli.BPop("test", 10);
	fmt.Println(msg)
	fmt.Println(err)
}

// Remove 例子
func ExampleClient_Remove() {
	cli := Client{
		Host:     "127.0.0.1",
		Port:     "6379",
		Database: 0,
		Password: "",
	}
	cli.Init()
	ok, err := cli.Remove("9a8482a06630840ce7da9da62d748b8a")
	fmt.Println(ok)
	fmt.Println(err)
}
